package org.linlinjava.litemall.wx.service;

import static org.linlinjava.litemall.wx.util.WxResponseCode.USER_APPROVE_INVALID;
import static org.linlinjava.litemall.wx.util.WxResponseCode.USER_APPROVE_UNKNOWN;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.linlinjava.litemall.core.util.ConvertUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.constant.DBConmonConstant;
import org.linlinjava.litemall.db.domain.LitemallUserApprove;
import org.linlinjava.litemall.db.service.LitemallUserApproveService;
import org.linlinjava.litemall.db.service.LitemallUserService;
import org.linlinjava.litemall.db.service.QualificationsService;
import org.linlinjava.litemall.wx.vo.QualificationsVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.google.common.collect.Lists;

import cn.hutool.core.map.MapUtil;

@Service
public class WxQualificationsService {
	@Autowired
	private LitemallUserService userService;
	@Autowired
	private LitemallUserApproveService userApproveService;
	@Autowired
	private QualificationsService qualificationsService;

	public List<LitemallUserApprove> queryList(Integer userId, Byte status, Integer page, Integer size, String sort,
			String order) {
		return userApproveService.queryList(userId, status, page, size, sort, order);
	}

	/**
	 * 获取资质信息
	 * 
	 * @param userId
	 * @return
	 */
	public Map<String, Object> getShopAuthInfo(Integer userId) {
		List<String> querys = Lists.newArrayList();
		querys.add(" u.id = " + userId + " ");
		querys.add(" s.deleted = 0 ");
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		Map<String, Object> userVo = qualificationsService.getShopAuthInfo(query);
		if (userVo != null) {
			return MapUtil.toCamelCaseMap(userVo);
		}
		return userVo;
	}

	/**
	 * 获取资质信息
	 * 
	 * @param userId
	 * @return
	 */
	public QualificationsVo getQualificationsVo(Integer userId) {
		List<String> querys = Lists.newArrayList();
		querys.add(" u.id = " + userId + " ");
		querys.add(" s.deleted = 0 ");
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		QualificationsVo qualificationsVo = null;
		Map<String, Object> userVo = qualificationsService.getShopAuthInfo(query);
		if (userVo != null) {
			qualificationsVo = ConvertUtils.mapToObject(MapUtil.toCamelCaseMap(userVo), QualificationsVo.class);
		}
		return qualificationsVo;
	}

	/**
	 * 获取资质信息
	 * 
	 * @param userId
	 * @return
	 */
	public Object getQualificationsInfo(Integer userId) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		Map<String, Object> result = new HashMap<>();
		result.put("userVo", getShopAuthInfo(userId));
		return ResponseUtil.ok(result);
	}

	public Object detail(Integer userId, Integer authId, Integer isAuth) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		if (1 == isAuth) {
			// 已认证资质明细
			return authDetail(userId);
		} else {
			// 审批信息资质明细
			return approveDetail(userId, authId);
		}
	}

	public Object authDetail(Integer userId) {
		List<String> querys = Lists.newArrayList();
		querys.add(" u.id = " + userId + " ");
		querys.add(" s.deleted = 0 ");
		String query = StringUtils.collectionToDelimitedString(querys, "and");
		Map<String, Object> userVo = qualificationsService.getShopAuthInfo(query);
		if(userVo == null) {
			return ResponseUtil.fail();
		}
		userVo = MapUtil.toCamelCaseMap(userVo);

		userVo.put("herbInfoList", Lists.newArrayList());
		if (DBConmonConstant.UserType.Personal.getValue() == MapUtils.getByte(userVo, "userType").byteValue()) {
			List<String> herbQuerys = Lists.newArrayList();
			herbQuerys.add(" u.id = " + userId + " ");
			herbQuerys.add(" IFNULL(h.generic_name,'') !='' ");
			herbQuerys.add(" h.deleted = 0 ");
			herbQuerys.add(" uha.deleted = 0 ");
			String herbQuery = StringUtils.collectionToDelimitedString(herbQuerys, "and");
			List<Map> herbInfoList = qualificationsService.getUserHerbGenericNames(herbQuery);
			if (CollectionUtils.isNotEmpty(herbInfoList)) {
				userVo.put("herbInfoList", ConvertUtils.sourceToCamelCaseMap(herbInfoList));
			}
		}

		Map<String, Object> result = new HashMap<>();
		result.put("userApproveInfo", userVo);

		return ResponseUtil.ok(result);
	}

	/**
	 * 认证信息详情
	 *
	 * @param userId 用户ID
	 * @param authId 认证ID
	 * @return 认证信息
	 */
	public Object approveDetail(Integer userId, Integer authId) {
		// 认证信息
		LitemallUserApprove userApprove = userApproveService.findById(userId, authId);
		if (null == userApprove) {
			return ResponseUtil.fail(USER_APPROVE_UNKNOWN, "订单不存在");
		}
		if (!userApprove.getUserId().equals(userId)) {
			return ResponseUtil.fail(USER_APPROVE_INVALID, "不是当前用户的认证信息");
		}
		Map<String, Object> userApproveVo = new HashMap<String, Object>();
		userApproveVo.put("id", userApprove.getId());
		userApproveVo.put("contactName", userApprove.getContactName());
		userApproveVo.put("contactTel", userApprove.getContactTel());
		userApproveVo.put("contactAddress", userApprove.getContactAddress());
		userApproveVo.put("userType", userApprove.getUserType());
		userApproveVo.put("companyName", userApprove.getCompanyName());
		userApproveVo.put("companyLogo", userApprove.getCompanyLogo());
		userApproveVo.put("adminUsername", userApprove.getAdminUsername());
		userApproveVo.put("adminPassword", userApprove.getAdminPassword());
		userApproveVo.put("certificatesImage", userApprove.getCertificatesImage());
		userApproveVo.put("idCard1Url", userApprove.getIdCard1Url());
		userApproveVo.put("idCard2Url", userApprove.getIdCard2Url());

		userApproveVo.put("approveStatus", userApprove.getApproveStatus());
		userApproveVo.put("approveTime", userApprove.getApproveTime());
		userApproveVo.put("approveRemark", userApprove.getApproveRemark());

		userApproveVo.put("herbInfoList", Lists.newArrayList());
		if (DBConmonConstant.UserType.Personal.getValue() == userApprove.getUserType()) {
			List<String> querys = Lists.newArrayList();
			querys.add(" uhai.user_approve_id = " + userApprove.getId() + " ");
			querys.add(" IFNULL(h.generic_name,'') !='' ");
			querys.add(" h.deleted = 0 ");
			String query = StringUtils.collectionToDelimitedString(querys, "and");
			List<Map> herbInfoList = qualificationsService.getApproveHerbs(query);
			if (CollectionUtils.isNotEmpty(herbInfoList)) {
				userApproveVo.put("herbInfoList", ConvertUtils.sourceToCamelCaseMap(herbInfoList));
			}
		}

		Map<String, Object> result = new HashMap<>();
		result.put("userApproveInfo", userApproveVo);

		return ResponseUtil.ok(result);

	}

	public long countByUserIdAndStatus(Integer userId, Byte status) {
		return userApproveService.countByUserIdAndStatus(userId, status);
	}
}